// 所有Node.js API都可以在预加载过程中使用。
// 它拥有与Chrome扩展一样的沙盒。
const {contextBridge, ipcRenderer}  = require('electron')

console.log('Preload script loaded');

window.addEventListener('DOMContentLoaded', () => {
    console.log('DOMContentLoaded event fired in preload');
    const replaceText = (selector, text) => {
        const element = document.getElementById(selector)
        if (element) element.innerText = text
    }

    for (const dependency of ['chrome', 'node', 'electron']) {
        replaceText(`${dependency}-version`, process.versions[dependency])
    }
})

const electronApi = {
    appReady: () => {
        console.log('appReady called');
        ipcRenderer.send('app-ready')
    },
    // 添加获取路径的方法
    getPath: (name) => {
        console.log('getPath called with:', name);
        return ipcRenderer.invoke('get-path', name)
    },
    // 添加打开目录选择对话框的方法
    openDirectoryDialog: () => {
        console.log('openDirectoryDialog called');
        return ipcRenderer.invoke('open-directory-dialog')
    },
    // 获取应用程序路径
    getAppPath: () => {
        console.log('getAppPath called');
        return ipcRenderer.invoke('get-app-path')
    }
};

// 暴露API给渲染进程
contextBridge.exposeInMainWorld('electronApi', electronApi);

console.log('Electron API exposed to window.electronApi');